/* -*-c++-*- */
/* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
 * Copyright 2008-2010 Pelican Mapping
 * http://osgearth.org
 *
 * osgEarth is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 */
#ifndef OSGEARTH_DRIVER_SQLITE3_CACHE_DRIVEROPTIONS
#define OSGEARTH_DRIVER_SQLITE3_CACHE_DRIVEROPTIONS 1

#include <VBF_3DMap/VBF_Terrain/Common.h>
#include <VBF_3DMap/VBF_Terrain/Caching>
#include <VBF_3DMap/VBF_Terrain/TileSource>

// there is leak using the cache if compressed. It's resolved in
// rev 11609 of osg svn

namespace osgEarth { namespace Drivers
{
    using namespace osgEarth;

    class Sqlite3CacheOptions : public CacheOptions // NO EXPORT; header only
    {
    public:
        /**
         * Pathname of the database file.
         */
        CVBF_Optional<std::string>& path() { return _path; }
        const CVBF_Optional<std::string>& path() const { return _path; }

        CVBF_Optional<bool>& asyncWrites() { return _useAsyncWrites; }
        const CVBF_Optional<bool>& asyncWrites() const { return _useAsyncWrites; }

        CVBF_Optional<bool>& serialized() { return _serialized; }
        const CVBF_Optional<bool>& serialized() const { return _serialized; }

        CVBF_Optional<unsigned int>& maxSize() { return _maxSize; }
        const CVBF_Optional<unsigned int>& maxSize() const { return _maxSize; }


    public:
        Sqlite3CacheOptions( const ConfigOptions& options =ConfigOptions() )
            : CacheOptions( options ),
              _useAsyncWrites( true ), 
              _serialized( false ),
              _maxSize(100)
        {
            setDriver( "sqlite3" );
            fromConfig( _conf );
        }

        Config getConfig() const {
            Config conf = CacheOptions::getConfig();
            conf.updateIfSet( "path", _path );
            conf.updateIfSet( "async_writes", _useAsyncWrites );
            conf.updateIfSet( "serialized", _serialized );
            conf.updateIfSet( "max_size", _maxSize );
            return conf;
        }

        void mergeConfig( const Config& conf ) {
            CacheOptions::mergeConfig( conf );
            fromConfig( conf );
        }

        void fromConfig( const Config& conf ) {
            conf.getIfSet( "path", _path );
            conf.getIfSet( "async_writes", _useAsyncWrites );
            conf.getIfSet( "serialized", _serialized );
            conf.getIfSet( "max_size", _maxSize );
        }

        CVBF_Optional<std::string> _path;
        CVBF_Optional<bool> _useAsyncWrites;
        CVBF_Optional<bool> _serialized;
        CVBF_Optional<unsigned int>_maxSize; // layer - MB
    };

} } // namespace osgEarth::Drivers

#endif // OSGEARTH_DRIVER_SQLITE3_CACHE_DRIVEROPTIONS

